package api

import (
	"net/http"

	"example.com/example/dao"
	"example.com/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configCustomProcessBaseMachineRelRouter(router *httprouter.Router) {
	router.GET("/customprocessbasemachinerel", GetAllCustomProcessBaseMachineRel)
	router.POST("/customprocessbasemachinerel", AddCustomProcessBaseMachineRel)
	router.GET("/customprocessbasemachinerel/:argBaseID/:argMachineID", GetCustomProcessBaseMachineRel)
	router.PUT("/customprocessbasemachinerel/:argBaseID/:argMachineID", UpdateCustomProcessBaseMachineRel)
	router.DELETE("/customprocessbasemachinerel/:argBaseID/:argMachineID", DeleteCustomProcessBaseMachineRel)
}

func configGinCustomProcessBaseMachineRelRouter(router gin.IRoutes) {
	router.GET("/customprocessbasemachinerel", ConverHttprouterToGin(GetAllCustomProcessBaseMachineRel))
	router.POST("/customprocessbasemachinerel", ConverHttprouterToGin(AddCustomProcessBaseMachineRel))
	router.GET("/customprocessbasemachinerel/:argBaseID/:argMachineID", ConverHttprouterToGin(GetCustomProcessBaseMachineRel))
	router.PUT("/customprocessbasemachinerel/:argBaseID/:argMachineID", ConverHttprouterToGin(UpdateCustomProcessBaseMachineRel))
	router.DELETE("/customprocessbasemachinerel/:argBaseID/:argMachineID", ConverHttprouterToGin(DeleteCustomProcessBaseMachineRel))
}

// GetAllCustomProcessBaseMachineRel is a function to get a slice of record(s) from custom_process_base_machine_rel table in the custom_process database
// @Summary Get list of CustomProcessBaseMachineRel
// @Tags CustomProcessBaseMachineRel
// @Description GetAllCustomProcessBaseMachineRel is a handler to get a slice of record(s) from custom_process_base_machine_rel table in the custom_process database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.CustomProcessBaseMachineRel}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbasemachinerel [get]
// http "http://localhost:8080/customprocessbasemachinerel?page=0&pagesize=20" X-Api-User:user123
func GetAllCustomProcessBaseMachineRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "custom_process_base_machine_rel", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllCustomProcessBaseMachineRel(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetCustomProcessBaseMachineRel is a function to get a single record from the custom_process_base_machine_rel table in the custom_process database
// @Summary Get record from table CustomProcessBaseMachineRel by  argBaseID  argMachineID
// @Tags CustomProcessBaseMachineRel
// @ID argBaseID
// @ID argMachineID
// @Description GetCustomProcessBaseMachineRel is a function to get a single record from the custom_process_base_machine_rel table in the custom_process database
// @Accept  json
// @Produce  json
// @Param  argBaseID path int64 true "base_id"
// @Param  argMachineID path int64 true "machine_id"
// @Success 200 {object} model.CustomProcessBaseMachineRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /customprocessbasemachinerel/{argBaseID}/{argMachineID} [get]
// http "http://localhost:8080/customprocessbasemachinerel/1/1" X-Api-User:user123
func GetCustomProcessBaseMachineRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argMachineID, err := parseUint64(ps, "argMachineID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_machine_rel", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetCustomProcessBaseMachineRel(ctx, argBaseID, argMachineID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddCustomProcessBaseMachineRel add to add a single record to custom_process_base_machine_rel table in the custom_process database
// @Summary Add an record to custom_process_base_machine_rel table
// @Description add to add a single record to custom_process_base_machine_rel table in the custom_process database
// @Tags CustomProcessBaseMachineRel
// @Accept  json
// @Produce  json
// @Param CustomProcessBaseMachineRel body model.CustomProcessBaseMachineRel true "Add CustomProcessBaseMachineRel"
// @Success 200 {object} model.CustomProcessBaseMachineRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbasemachinerel [post]
// echo '{"base_id": 73,"machine_id": 73}' | http POST "http://localhost:8080/customprocessbasemachinerel" X-Api-User:user123
func AddCustomProcessBaseMachineRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	customprocessbasemachinerel := &model.CustomProcessBaseMachineRel{}

	if err := readJSON(r, customprocessbasemachinerel); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := customprocessbasemachinerel.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	customprocessbasemachinerel.Prepare()

	if err := customprocessbasemachinerel.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_machine_rel", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	customprocessbasemachinerel, _, err = dao.AddCustomProcessBaseMachineRel(ctx, customprocessbasemachinerel)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, customprocessbasemachinerel)
}

// UpdateCustomProcessBaseMachineRel Update a single record from custom_process_base_machine_rel table in the custom_process database
// @Summary Update an record in table custom_process_base_machine_rel
// @Description Update a single record from custom_process_base_machine_rel table in the custom_process database
// @Tags CustomProcessBaseMachineRel
// @Accept  json
// @Produce  json
// @Param  argBaseID path int64 true "base_id"// @Param  argMachineID path int64 true "machine_id"
// @Param  CustomProcessBaseMachineRel body model.CustomProcessBaseMachineRel true "Update CustomProcessBaseMachineRel record"
// @Success 200 {object} model.CustomProcessBaseMachineRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbasemachinerel/{argBaseID}/{argMachineID} [put]
// echo '{"base_id": 73,"machine_id": 73}' | http PUT "http://localhost:8080/customprocessbasemachinerel/1/1"  X-Api-User:user123
func UpdateCustomProcessBaseMachineRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argMachineID, err := parseUint64(ps, "argMachineID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	customprocessbasemachinerel := &model.CustomProcessBaseMachineRel{}
	if err := readJSON(r, customprocessbasemachinerel); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := customprocessbasemachinerel.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	customprocessbasemachinerel.Prepare()

	if err := customprocessbasemachinerel.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_machine_rel", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	customprocessbasemachinerel, _, err = dao.UpdateCustomProcessBaseMachineRel(ctx,
		argBaseID, argMachineID,
		customprocessbasemachinerel)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, customprocessbasemachinerel)
}

// DeleteCustomProcessBaseMachineRel Delete a single record from custom_process_base_machine_rel table in the custom_process database
// @Summary Delete a record from custom_process_base_machine_rel
// @Description Delete a single record from custom_process_base_machine_rel table in the custom_process database
// @Tags CustomProcessBaseMachineRel
// @Accept  json
// @Produce  json
// @Param  argBaseID path int64 true "base_id"// @Param  argMachineID path int64 true "machine_id"
// @Success 204 {object} model.CustomProcessBaseMachineRel
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /customprocessbasemachinerel/{argBaseID}/{argMachineID} [delete]
// http DELETE "http://localhost:8080/customprocessbasemachinerel/1/1" X-Api-User:user123
func DeleteCustomProcessBaseMachineRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argMachineID, err := parseUint64(ps, "argMachineID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_machine_rel", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeleteCustomProcessBaseMachineRel(ctx, argBaseID, argMachineID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
